﻿using mozhi.smarterp.BasicData.Employees.Repositories;
using mozhi.smarterp.BasicData.Employees.Services;
using Mozhi.Abp.Domain.AuthorCenter;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace mozhi.smarterp.BasicData.Employees
{
    public class EmployeeAppService : BasicDataAppService, IEmployeeAppService
    {
        private IEmployeeRepository _repository;
        private IGuidGenerator _guidGenerator;
        private EmployeeManager _manager;
        private IDepartmentRepository _departmentRepository;

        public EmployeeAppService(IEmployeeRepository repository, IGuidGenerator guidGenerator, EmployeeManager manager , IDepartmentRepository departmentRepository)
        {
            _repository = repository;
            _guidGenerator = guidGenerator;
            _manager = manager;
            _departmentRepository = departmentRepository;
        }

        public async Task<EmployeeDto> CreateAsync(EmployeeCreateDto input)
        {
            var employee = await _manager.GetAysnc(null, input.Code, input.Name, input.DepartmentId, input.Phone, input.Email, input.Activated);

            employee = await _repository.InsertAsync(employee);

            var result = ObjectMapper.Map<Employee , EmployeeDto>(employee);

            await LoadDepartmentInfoAsync(new List<EmployeeDto> { result });

            return result;
        }

        public async Task DeleteAsync(Guid id)
        {
           var employee = await _repository.GetAsync(id);

            if (employee == null)
                throw new BusinessException($"员工：{id}不存在", "100000");

            await _repository.DeleteAsync(id);
        }

        public async Task<EmployeeDto> GetAsync(Guid id)
        {
            var employee = await _repository.GetAsync(id);

            var result = ObjectMapper.Map<Employee, EmployeeDto>(employee);

            await LoadDepartmentInfoAsync(new List<EmployeeDto> { result });

            return result;
        }

        public async Task<PagedResultDto<EmployeeDto>> GetListAsync(EmployeePagedResultRequestDto filter)
        {
            var employees = await _repository.GetPagedListAsync(filter.Keyword , filter.Activated , filter.DepartmentId ,filter.SkipCount, filter.MaxResultCount, filter.Sorting ?? nameof(Employee.Name));

            var count = await _repository.GetCountAsync(filter.Keyword, filter.Activated, filter.DepartmentId);

            var result = new PagedResultDto<EmployeeDto>()
            {
                Items = ObjectMapper.Map<List<Employee>, List<EmployeeDto>>(employees),
                TotalCount = count
            };

            await LoadDepartmentInfoAsync((List<EmployeeDto>)result.Items);

            return result;
        }

        public async Task<EmployeeDto> UpdateAsync(EmployeeUpdateDto input)
        {
            var employee = await _manager.GetAysnc(input.Id, input.Code, input.Name, input.DepartmentId , input.Phone , input.Email, input.Activated);

            employee = await _repository.UpdateAsync(employee);

            var result = ObjectMapper.Map<Employee, EmployeeDto>(employee);

            return result;
        }

        #region
        private async Task LoadDepartmentInfoAsync(List<EmployeeDto> employees)
        {
            foreach (var employee in employees)
            {
                if (!employee.DepartmentId.HasValue)
                    continue;

                var department = await _departmentRepository.GetAsync(employee.DepartmentId.Value);

                employee.DepartmentName = department?.Name;
            }
        }
        #endregion
    }
}
